﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace DigitalQuran.Extensions {

    public static class ListExtensions {

        /// <summary>
        /// Determines weather any value of list contains the specified value
        /// </summary>
        /// <param name="source">List of Itmes</param>
        /// <param name="value">String to search</param>
        /// <param name="comparisonType">comparison type to use</param>
        /// <returns></returns>
        public static bool ItemContains(this IList<string> source, string value, StringComparison comparisonType) {
            foreach (string item in source) {
                if (item.Contains(value, comparisonType))
                    return true;
            }
            return false;
        }

        public static bool ItemStartsWith(this IList<string> source, string value, StringComparison comparisonType) {
            foreach (string item in source) {
                if (item.StartsWith(value, comparisonType)) {
                    return true;
                }
            }
            return false;
        }

        public static IList<object> Intersect(this IList<object> source, IList<object> target) {
            IList<object> result = new List<object>();
            foreach (object item in source) {
                if (target.Contains(item)) {
                    result.Add(item);
                }
            }
            return result;
        }
        public static IList<object> Union(this IList<object> source, IList<object> target) {
            IList<object> result = new List<object>(source);
            foreach (object item in target) {
                if (!source.Contains(item)) {
                    result.Add(item);
                }
            }
            return result;
        }

        public static void QuickSort(this IList<long> list, int min, int max) {
            if (list == null || list.Count < 1 || min > max || (min < 0) || (max >= list.Count)) return;

            int lo = min;
            int hi = max;

            var mid = list[(lo + hi) / 2];	// uses copy constructor

            do {
                while (list[lo] < mid)		// uses comparison operator
                    lo++;
                while (mid < list[hi])
                    hi--;

                if (lo <= hi) {
                    var temp = list[hi];
                    list[hi] = list[lo];
                    list[hi] = temp;
                    lo++;
                    hi--;
                }
            }
            while (lo <= hi);

            if (hi > min)
                QuickSort(list, min, hi);
            if (lo < max)
                QuickSort(list, lo, max);
        }

        public static string ToString(this IEnumerable container, string seprater) {
            var builder = new StringBuilder();

            foreach (var item in container) {
                builder.Append(item);
                builder.Append(seprater);
            }

            if (builder.Length > seprater.Length)
                builder.Remove(builder.Length - seprater.Length, seprater.Length);            

            return builder.ToString();
        }

        public static void AddRange(this List<int> list, int start, int end) {
            for (int i = start; i < end; i++)
                list.Add(i);
        }
    }
}